package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.pojo.User;
import com.example.demo.service.ThreadTestService;
import com.example.demo.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;

    @Resource
    ThreadTestService threadTestService;

    @ApiOperation("测试接口")
    @GetMapping(value = "/getList")
    public Page<User> getList(@RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "5") Integer size, String name) {
        QueryWrapper qw = new QueryWrapper();
        if (!ObjectUtils.isEmpty(name)) {
            qw.like("name", name);
        }
        return userService.pageMaps(new Page<>(current, size), qw);
    }

    @GetMapping(value = "/getByID/{id}")
    public Object getByID(@PathVariable String id) {
        return userService.getById(id);
    }

    @GetMapping(value = "/listByName/{name}")
    public Object listByName(@PathVariable String name) {
        List<User> users = userService.list((Wrapper<User>) new QueryWrapper().like("name", name));
        Map data = new Hashtable<>();
        data.put("data", users);
        return data;
    }

    @PostMapping("/addOrEdit")
    public Object addOrEdit(@RequestBody User user) {
        return userService.saveOrUpdate(user);
    }

    @DeleteMapping("/del/{id}")
    public Object del(@PathVariable String id) {
        return userService.removeById(id);
    }

    @GetMapping("test")
    public Object test() {
        Map data = new HashMap();
        data.put("code", 1001);
        data.put("msg", "ok");
        return data;
    }

}
